package com.ycMc.controller;

import com.ycMc.bean.request.ProductRequest;
import com.ycMc.bean.response.ListResponse;
import com.ycMc.bean.response.ObjectResponse;
import com.ycMc.common.bean.Request;
import com.ycMc.common.bean.Response;
import com.ycMc.domain.Product;
import com.ycMc.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by libo2 on 2017/8/23.
 */
@Controller
@RequestMapping("api/product")
public class ProductController {

    @Autowired
    private ProductService productService;


    @RequestMapping("insert")
    @ResponseBody
    /**
    *Create by libo2
    *Time:2017/8/24 on 21:48
    *@Request :[request]
    *@Response:com.ycMc.common.bean.Response
    **/
    public Response insert(ProductRequest request){
        Response response = new Response();
        try {
            Product product = new Product();
            BeanUtils.copyProperties(request, product);
            productService.insert(product);
            response.setStatusCode(Response.SUCCESS);
            response.setMsg("success");
        }catch (Exception e){
            e.printStackTrace();
            response.setStatusCode(Response.FAILED);
            response.setMsg(e.getMessage());
        }
        return  response;
    }


    @RequestMapping("delete")
    @ResponseBody
    /**
    *Create by libo2
    *Time:2017/8/24 on 21:49
    *@Request :[request]
    *@Response:com.ycMc.common.bean.Response   @Param = productId
    **/
    public Response delete(ProductRequest request){
        Response response = new Response();
        try {
            productService.delete(request.getProductId());
            response.setStatusCode(Response.SUCCESS);
            response.setMsg("success");
        }catch (Exception e){
            e.printStackTrace();
            response.setStatusCode(Response.FAILED);
            response.setMsg(e.getMessage());
        }
        return  response;
    }



    @RequestMapping("update")
    @ResponseBody
    public Response update(ProductRequest request){
        Response response = new Response();
        try {
            Product product = new Product();
            BeanUtils.copyProperties(request, product);
            productService.update(product);
            response.setStatusCode(Response.SUCCESS);
            response.setMsg("success");
        }catch (Exception e){
            e.printStackTrace();
            response.setStatusCode(Response.FAILED);
            response.setMsg(e.getMessage());
        }
        return  response;
    }



    @RequestMapping("selectAll")
    @ResponseBody
    public ListResponse selectAll(Request request){
        ListResponse response = new ListResponse();
        try {
            response.setCommonList(
                    productService.selectAll(request.getCurrentPage(), request.getPageSize(),request.getContainZero()));
            response.setStatusCode(Response.SUCCESS);
            response.setMsg("success");
        }catch (Exception e){
            e.printStackTrace();
            response.setStatusCode(Response.FAILED);
            response.setMsg(e.getMessage());
        }
        return  response;
    }



    @RequestMapping("selectOne")
    @ResponseBody
    public ObjectResponse selectOne(ProductRequest request){
        ObjectResponse response = new ObjectResponse();
        try {
            response.setObject(   productService.selectOne(request.getProductId()));
            response.setStatusCode(Response.SUCCESS);
            response.setMsg("success");
        }catch (Exception e){
            e.printStackTrace();
            response.setStatusCode(Response.FAILED);
            response.setMsg(e.getMessage());
        }
        return  response;
    }



    @RequestMapping("select")
    @ResponseBody
    public ListResponse select(ProductRequest request){
        ListResponse response = new ListResponse();
        try {
            Product product = new Product();
            BeanUtils.copyProperties(request, product);
            response.setCommonList(
                    productService.select(product, request.getCurrentPage(), request.getPageSize()));
            response.setStatusCode(Response.SUCCESS);
            response.setMsg("success");
        }catch (Exception e){
            e.printStackTrace();
            response.setStatusCode(Response.FAILED);
            response.setMsg(e.getMessage());
        }
        return  response;
    }


    @RequestMapping("query")
    @ResponseBody
    public ListResponse query(ProductRequest request){
        ListResponse response = new ListResponse();
        try {

            response.setCommonList(
                    productService.query(request.getKeyword(), request.getCurrentPage(), request.getPageSize(),request.getContainZero()));
            response.setStatusCode(Response.SUCCESS);
            response.setMsg("success");
        }catch (Exception e){
            e.printStackTrace();
            response.setStatusCode(Response.FAILED);
            response.setMsg(e.getMessage());
        }
        return  response;
    }



    @RequestMapping("aaaaaa")
    @ResponseBody
    public Response aaaa(Request request){
        productService.aaaaa();
        return null;
    }
}
